/*
 * Copyright 2008 the original author Ang Gao.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * file.java 2009-2-17
 */
package ie.ucc.cs.anggao.finalproject.client.support.searchengine;

/**
 * @author GaoAng
 *
 */


import ie.ucc.cs.anggao.finalproject.client.model.SearchEngine;
import ie.ucc.cs.anggao.finalproject.client.model.SearchEngineResult;
import ie.ucc.cs.anggao.finalproject.client.support.async.JSONRequest;
import ie.ucc.cs.anggao.finalproject.client.support.async.JSONRequestHandler;
import ie.ucc.cs.anggao.finalproject.client.ui.event.QueryEvent;
import ie.ucc.cs.anggao.finalproject.client.ui.mainpanel.infopane.InfoPane;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;

public class FlickrSearcher extends BaseSearcher implements JSONRequestHandler {
	
	private String query;
	
	public FlickrSearcher( InfoPane view ){
		super( view, new SearchEngine( "Flickr Search", "http://www.flickr.com" ) );
	}
	
	public void doIR( QueryEvent event ){
		String q = event.getQuery();
		query = q;
		JSONRequest.get( "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=85618ad7d326d8ef93c6bee9ed32706f&per_page=5&format=json&text="+q, "jsonFlickrApi", this  );

	}
	
	public void doIR( String query ){
		this.query = query;
		JSONRequest.get( "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=85618ad7d326d8ef93c6bee9ed32706f&per_page=5&format=json&text="+query, "jsonFlickrApi", this  );

	}
	
	private String removeterm(String query){
		String [] terms = query.split(" ");
		String newq = "";
		for(int i = terms.length-1; i > 0 ; i--){
			newq = terms[i] + " " + newq;
		}
		
		System.out.println("----------Image query is "+ newq);
		
		return newq;
	}
	
	public void onRequestComplete( JavaScriptObject json ){
		JSONObject j = new JSONObject(json);//(JSONObject)JSONParser.parse(json);
		JSONObject root = (JSONObject)j.get("photos");
		JSONArray entries = (JSONArray)root.get("photo");
		
		if(entries.size() <= 0){
			String newquery = removeterm(this.query);
			this.doIR(newquery);
			return;
		}
		
		getView().clearResults();
		for( int i=0; i< entries.size(); ++i ){
			JSONObject entry = (JSONObject)entries.get(i);
			JSONString title = (JSONString)entry.get("title");
			JSONNumber farm = (JSONNumber)entry.get("farm");
			JSONString server = (JSONString)entry.get("server");
			JSONString id = (JSONString)entry.get("id");
			JSONString owner = (JSONString)entry.get("owner");
			JSONString secret = (JSONString)entry.get("secret");
			String imageUrl = "http://farm"+((int)farm.getValue())+".static.flickr.com/"+server.stringValue()+"/"+id.stringValue()+"_"+secret.stringValue()+"_m.jpg";
			String htmlUrl = "http://www.flickr.com/photos/"+owner.stringValue()+"/"+id.stringValue();
			
			getView().addSearchResult(
					new SearchEngineResult( title.stringValue(),htmlUrl, "<img src='"+imageUrl+"'/>" ) ,i+1);

		}
	}
}
